<style scoped>
    .layui-form >>> .layui-form-select{
        z-index: inherit;
    }
</style>

<template>
    <div class="company">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>添加种植户</legend>
        </fieldset>

        <form class="layui-form " >
            <div class="layui-form-item">
                <label class="layui-form-label">种植户</label>
                <div class="layui-input-block">
                    <input type="hidden" name="entity.name" ref="farmername" />
                    <select type="text" name="entity.userid" lay-filter="farmer" lay-verify="required" lay-search class="layui-select" >
                        <option value="">请选择</option>
                        <option v-for="(typ, index) in farmers" :value="typ.guid">{{typ.realname}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所属部门</label>
                <div class="layui-input-block">
                    <input type="hidden" name="entity.depname" ref="deptname" />
                    <select type="text" lay-filter="depart" name="entity.deptguid" lay-verify="required" class="layui-select" >
                        <option value=""></option>
                        <option v-for="(typ, index) in departments" :value="typ.guid">{{typ.name}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所在地址</label>
                <div class="layui-inline" style="width: 155px">
                    <select name="entity.province" id="province" lay-verify="required"   lay-search lay-filter="province">
                        <option value="">省份</option>
                    </select>
                </div>
                <div class="layui-inline" style="width: 155px">
                    <select name="entity.city" id="city" lay-verify="required"    lay-search lay-filter="city">

                    </select>
                </div>
                <div class="layui-inline" style="width: 161px">
                    <select name="entity.district" id="district" lay-verify="required"    lay-search>

                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">详细地址</label>
                <div class="layui-input-block">
                    <input type="text" name="entity.address" lay-verify="required" placeholder="请输入详细地址" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">联系电话</label>
                <div class="layui-input-block">
                    <input type="number" name="entity.telephone" lay-verify="required" placeholder="请输入联系电话" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">银行账户名</label>
                <div class="layui-input-block">
                    <input type="text" name="entity.accountname" placeholder="请输入银行账户名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-block">
                    <input type="text" name="entity.accountno" placeholder="请输入银行账号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">开户行</label>
                <div class="layui-input-block">
                    <input type="text" name="entity.bankname" placeholder="请输入开户行" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">有无仓库</label>
                <div class="layui-input-block">
                    <input type="radio" lay-filter="selfstore" name="entity.selfstore" value="1" title="有">
                    <input type="radio" lay-filter="selfstore" name="entity.selfstore" checked value="2" title="无">
                </div>
            </div>
            <div class="layui-form-item" v-if="selfstore == 1">
                <div class="layui-form-label">仓库容量(吨)</div>
                <div class="layui-input-block">
                    <input class="layui-input" type="number" name="entity.storecapcity" placeholder="请输入仓库容量">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea name="entity.remark" placeholder="请输入备注内容" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                    <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
                </div>
            </div>
        </form>
    </div>
</template>

<script>
    export default {
        name: 'AddFarmerInfo',
        props: ['id'],
        data () {
            return {
                farmers: [],
                departments: [],
                selfstore: 2,
                userid: sessionStorage.userid
            }
        },
        mounted() {
            let that = this;
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;

                var province = $("#province"),
                    city = $("#city"),
                    district = $("#district");

                //初始将省份数据赋予
                for (var i = 0; i < provinceList.length; i++) {
                    addEle(province, provinceList[i].name);
                }

                //赋予完成 重新渲染select
                form.render('select');

                //向select中 追加内容
                function addEle(ele, value) {
                    var optionStr = "";
                    optionStr = "<option value=" + value + " >" + value + "</option>";
                    ele.append(optionStr);
                }

                //移除select中所有项 赋予初始值
                function removeEle(ele) {
                    ele.find("option").remove();
                    var optionStar = "<option value=" + "" + ">" + "请选择" + "</option>";
                    ele.append(optionStar);
                }

                var provinceText,
                    cityText,
                    cityItem;

                //选定省份后 将该省份的数据读取追加上
                form.on('select(province)', function(data) {
                    provinceText = data.value;
                    $.each(provinceList, function(i, item) {
                        if (provinceText == item.name) {
                            cityItem = i;
                            return cityItem;
                        }
                    });
                    removeEle(city);
                    removeEle(district);
                    $.each(provinceList[cityItem].cityList, function(i, item) {
                        addEle(city, item.name);
                    })
                    //重新渲染select
                    form.render('select');
                })
                ////选定市或直辖县后 将对应的数据读取追加上
                form.on('select(city)', function(data) {
                    cityText = data.value;
                    removeEle(district);
                    $.each(provinceList, function(i, item) {
                        if (provinceText == item.name) {
                            cityItem = i;
                            return cityItem;
                        }
                    });
                    $.each(provinceList[cityItem].cityList, function(i, item) {
                        if (cityText == item.name) {
                            for (var n = 0; n < item.areaList.length; n++) {
                                addEle(district, item.areaList[n]);
                            }
                        }
                    })
                    //重新渲染select
                    form.render('select');
                })

                //获取种植户
                that.getUsers();
                that.getDepartment();
                form.on('select(farmer)', function (data) {
                    that.$refs.farmername.value = that.checkObjNull(that.farmers.filter(item => {
                        return data.value == item.guid;
                    })[0]).realname;
                })
                form.on('radio(selfstore)', function (data) {
                    that.selfstore = data.value;
                })
                form.render('radio');

                form.on('select(depart)', function (data) {
                    that.$refs.deptname.value = that.checkObjNull(that.departments.filter(item => {
                        return data.value == item.guid;
                    })[0]).name;
                })
                //提交编辑过的数据
                form.on('submit(submit)', function (data) {
                    let loader = layer.load();
                    $.ajax({
                        url: '/farmer/Farmerinfo!save.action',
                        data: data.field,
                        type: "post",
                        dataType: "json",
                        success: function(res) {
                            if (res.msg == "success") {
                                that.toast("添加成功");
                                that.$parent.switchPage('FarmerInfo');
                            } else if (res.msg == 'fail') {
                                that.toast("种植户档案已存在，不能重复添加");
                            } else{
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                    return false;
                });
            });
        },
        updated() {
            layui.form.render();
        },
        methods: {
            getUsers: function () {
                let that = this;
                let postParam = {};
                postParam['entity.userType'] = 1;
                this.axios({
                    method: 'get',
                    url: '/system/Tuser!getUserAll.action',
                    params: postParam
                }).then(response => {
                    if (response.data.info != undefined && response.data.info.length > 0) {
                        that.farmers = response.data.info;
                    }  else {
                        this.toastText('获取种植户数据失败');
                    }
                });
            },
            getDepartment: function () {
                let that = this;
                this.axios({
                    method: 'get',
                    responseType: 'json',
                    url: '/sales/Sellform!findDepartment.action',
                    params: {sPermission: 41}
                }).then(response => {
                    if (response.data.status == "true") {
                        that.departments = response.data.result;
                    } else if (response.data.status == "false") {
                        this.toastText(response.data.result);
                    }  else {
                        this.toastText('获取部门数据失败');
                    }
                });
            },
            back: function () {
                this.$parent.switchPage(this.$parent.refView);
            },

        }
    }
</script>
